/**
 * 213. 打家劫舍II
 */
public class Exerciser2 {
    public int rob(int[] nums) {
        int n = nums.length;
        return Math.max(nums[0] + rob(nums,2,n - 2),rob(nums,1,n -1));
    }

    public int rob(int[] nums,int left,int right) {
        if(left > right){
            return 0;
        }
        int n = nums.length;
        int[] f = new int[n];
        int[] g = new int[n];
        f[left] = nums[left];
        g[left] = 0;
        for(int i = left + 1;i <= right;i++) {
            f[i] = g[i - 1] + nums[i];
            g[i] = Math.max(f[i - 1],g[i - 1]);
        }
        return Math.max(f[right],g[right]);
    }
}
